package com.kxmall.web.controller.huishou;

import java.util.List;
import java.util.Arrays;

import com.kxmall.huishou.domain.bo.HsUserWasteBo;
import com.kxmall.huishou.domain.vo.HsUserWasteVo;
import com.kxmall.huishou.service.IHsUserWasteService;
import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.kxmall.common.annotation.RepeatSubmit;
import com.kxmall.common.annotation.Log;
import com.kxmall.common.core.controller.BaseController;
import com.kxmall.common.core.domain.PageQuery;
import com.kxmall.common.core.domain.R;
import com.kxmall.common.core.validate.AddGroup;
import com.kxmall.common.core.validate.EditGroup;
import com.kxmall.common.enums.BusinessType;
import com.kxmall.common.utils.poi.ExcelUtil;
import com.kxmall.huishou.domain.vo.HsAppointmentOrderVo;
import com.kxmall.huishou.domain.bo.HsAppointmentOrderBo;
import com.kxmall.huishou.service.IHsAppointmentOrderService;
import com.kxmall.common.core.page.TableDataInfo;

/**
 * 回收预约订单信息
 *
 * @author kxmall
 * @date 2023-11-23
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/huishou/appointmentOrder")
public class HsAppointmentOrderController extends BaseController {

    private final IHsAppointmentOrderService iHsAppointmentOrderService;

    private final IHsUserWasteService iHsUserWasteService;

    /**
     * 查询回收预约订单信息列表
     */
    @SaCheckPermission("huishou:appointmentOrder:list")
    @GetMapping("/list")
    public TableDataInfo<HsAppointmentOrderVo> list(HsAppointmentOrderBo bo, PageQuery pageQuery) {
        return iHsAppointmentOrderService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出回收预约订单信息列表
     */
    @SaCheckPermission("huishou:appointmentOrder:export")
    @Log(title = "回收预约订单信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HsAppointmentOrderBo bo, HttpServletResponse response) {
        List<HsAppointmentOrderVo> list = iHsAppointmentOrderService.queryList(bo);
        ExcelUtil.exportExcel(list, "回收预约订单信息", HsAppointmentOrderVo.class, response);
    }

    /**
     * 获取回收预约订单信息详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("huishou:appointmentOrder:query")
    @GetMapping("/{id}")
    public R<HsAppointmentOrderVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        HsAppointmentOrderVo hsAppointmentOrderVo = iHsAppointmentOrderService.queryById(id);
        if(hsAppointmentOrderVo != null){
            HsUserWasteBo hsUserWasteBo = new HsUserWasteBo();
            hsUserWasteBo.setOrderId(id);
            hsUserWasteBo.setOrderType(0l);
            List<HsUserWasteVo> hsUserWasteVoList = iHsUserWasteService.queryList(hsUserWasteBo);
            hsAppointmentOrderVo.setHsUserWasteVoList(hsUserWasteVoList);
        }
        return R.ok(hsAppointmentOrderVo);
    }

    /**
     * 新增回收预约订单信息
     */
    @SaCheckPermission("huishou:appointmentOrder:add")
    @Log(title = "回收预约订单信息", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody HsAppointmentOrderBo bo) {
        return toAjax(iHsAppointmentOrderService.insertByBo(bo));
    }

    /**
     * 修改回收预约订单信息
     */
    @SaCheckPermission("huishou:appointmentOrder:edit")
    @Log(title = "回收预约订单信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody HsAppointmentOrderBo bo) {
        return toAjax(iHsAppointmentOrderService.updateByBo(bo));
    }

    /**
     * 删除回收预约订单信息
     *
     * @param ids 主键串
     */
    @SaCheckPermission("huishou:appointmentOrder:remove")
    @Log(title = "回收预约订单信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iHsAppointmentOrderService.deleteWithValidByIds(Arrays.asList(ids), true));
    }
}
